<?php

/**

 * 订单管理

 * @author maniac

 */

namespace app\shop\controller;



use app\facade\Orders AS OrdersFacade;

use app\common\model\Orders AS OrdersModel;

use app\facade\UserLogs;



class Orders extends Base

{

    /**
     * 首页列表
     */
    public function index()
    {
        // 当前页数
        $page   = input('get.page/d', 1);
        // 模型
        $model  = $this->getModel();
        // 查询条件
        $where  = $this->where();
        // 排序条件
        $order  = $this->order();
        // 数据总数
        $count  = $model::where($where)->count();
        //数据总金额
        $pay_amount  = $model::where($where)->sum('pay_amount');
        // 每页条数
        $limit  = $this->limit();
        // 分页条件
        $offset = $this->offset($page, $limit);
        // 分页数据
        $data   = $model::field(true)->where($where)->order($order)->limit($offset)->select();
        // 模板
        $template = $this->indexTemplate();

        // 列表附加
        $this->indexAttach($data);

        return $this->fetch($template, [
            'page'  => $page,
            'count' => $count,
            'limit' => $limit,
            'data'  => $data,
            'pay_amount'  => $pay_amount,
        ]);
    }



    /**

     * 详情页附加

     * @param array $info 查看的信息

     */

    public function addAttach($info)

    {

        

    }

    

    /**

     * 组合 where 查询条件

     */

    public function where()

    {
		$shop_id =	$this->shop_id();

        $order_status_id = input('get.order_status_id/d', 0);

        $keywords        = input('get.keywords/s', '');

        $startTime       = input('get.start_time/s', '');

        $endTime         = input('get.end_time/s', '');

        $member_id         = input('get.member_id/s', '');

        $store         = input('get.store/s', '');

        $store_id         = input('get.store_id/s', '');

		$where[]        = [
			['store_id','eq',$shop_id],
		];

        if ($order_status_id) {

            switch ($order_status_id) {

                case 1:

                    // 已取消

                    $where[] = [

                        ['order_status', 'eq', 2],

                    ];

                    break;

                case 2:

                    // 未付款

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 0],

                    ];

                    break;

                case 3:

                    // 已付款

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 1],

                    ];

                    break;

                case 4:

                    // 待发货

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 1],

                        ['shipping_status', 'eq', 0],

                    ];

                    break;

                case 5:

                    // 待收货

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 1],

                        ['shipping_status', 'eq', 1],

                    ];

                    break;

                case 6:

                    $where[] = [

                        ['order_status', 'eq', 255],

                    ];

                    // 已完成

                    break;

            }

        }



        if ($member_id > 0) {

            $where[] = [

                ['userid','eq',$member_id],

            ];

        }
        if ($store_id > 0) {

            $where[] = [

                ['store_id', 'eq',$store_id],

            ];

        }

        

        if ($startTime) {

            $where[] = [

                ['create_time', 'egt', strtotime($startTime)],

            ];

        }

        if ($endTime) {

            $where[] = [

                ['create_time', 'lt', strtotime($endTime)],

            ];

        }

        

        $this->assign('order_status_id', $order_status_id);

        $this->assign('keywords', $keywords);

        $this->assign('startTime', $startTime);

        $this->assign('endTime', $endTime);

        $this->assign('member_id', $member_id);

        $this->assign('store', $store);

        $this->assign('store_id', $store_id);

        return $where;

    }

    

    /**

     * 导出 excel

     * @return [type] [description]

     */

    public function export()

    {

        $where = $this->where();

        UserLogs::insertLog(['title' => '导出订单列表']);

        OrdersFacade::export($where);

    }

    

    /**

     * 指派配送员

     */

    public function allot()

    {

        $id     = input('post.id/d', 0);

        $userid = input('post.userid/d', 0);

        if (!$id || !$userid) {

            return j(1, '指派失败');

        }

        try {

            $result = OrdersFacade::allot($id, $userid);

            if ($result) {

                return j(0, '指派成功');

            }

            return j(1, '指派失败');

        } catch (\Exception $e) {

            return j(1, $e->getMessage());

        }

    }

    

    /**

     * 订单操作

     */

    public function orderAction()

    {

        $data = input('post.');

        if(isset($data['pay_status']) &&$data['pay_status'] == 1){
            $data['pay_type'] = 8;
            $data['pay_time'] = time();
        }

        if (isset($data['shipping_status']) && $data['shipping_status'] == 2) {

            $data['order_status'] = 255;

        }

        OrdersModel::update($data);

        return j(0, '操作成功');

    }

}

